package com.dsz.apibackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsz.apibackend.common.*;
import com.dsz.apibackend.exception.BusinessException;
import com.dsz.apibackend.model.dto.interfaceInfo.InterfaceInfoAddRequest;
import com.dsz.apibackend.model.dto.interfaceInfo.InterfaceInfoInvokeRequest;
import com.dsz.apibackend.model.dto.interfaceInfo.InterfaceInfoQueryRequest;
import com.dsz.apibackend.model.dto.interfaceInfo.InterfaceInfoUpdateRequest;
import com.dsz.apibackend.service.InterfaceInfoService;
import com.dsz.apicommon.model.domain.InterfaceInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author dushangzhi
 */
@RestController
@RequestMapping("/interfaceInfo")
@CrossOrigin(origins = "http://localhost:8000", allowCredentials = "true")
public class InterfaceInfoController {
    @Resource
    private InterfaceInfoService interfaceInfoService;

    /**
     * 查询接口列表
     */
    @GetMapping("/list")
    public BaseResponse<List<InterfaceInfo>> listInterfaceInfo() {
        List<InterfaceInfo> interfaceInfoList = interfaceInfoService.list();
        return ResultUtils.success(interfaceInfoList);
    }

    /**
     * 分页查询接口列表
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<InterfaceInfo>> pageInterfaceInfo(@RequestBody InterfaceInfoQueryRequest interfaceInfoQueryRequest) {
        if (interfaceInfoQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<InterfaceInfo> interfaceInfoPage = interfaceInfoService.pageInterfaceInfo(interfaceInfoQueryRequest);
        return ResultUtils.success(interfaceInfoPage);
    }

    /**
     * 添加接口
     */
    @PostMapping("/add")
    public BaseResponse<Long> addInterfaceInfo(@RequestBody InterfaceInfoAddRequest interfaceInfoAddRequest, HttpServletRequest request) {
        if (interfaceInfoAddRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = interfaceInfoService.saveInterfaceInfo(interfaceInfoAddRequest, request);
        return ResultUtils.success(id);
    }

    /**
     * 修改接口
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateInterfaceInfo(@RequestBody InterfaceInfoUpdateRequest interfaceInfoUpdateRequest, HttpServletRequest request) {
        if (interfaceInfoUpdateRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = interfaceInfoService.updateInterfaceInfo(interfaceInfoUpdateRequest, request);
        return ResultUtils.success(res);
    }

    /**
     * 删除接口
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteInterfaceInfo(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = interfaceInfoService.deleteInterfaceInfo(deleteRequest, request);
        return ResultUtils.success(res);
    }

    /**
     * 发布接口
     */
    @PostMapping("/online")
    public BaseResponse<Boolean> onlineInterfaceInfo(@RequestBody IdRequest idRequest, HttpServletRequest request) {
        if (idRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = interfaceInfoService.onlineInterfaceInfo(idRequest, request);
        return ResultUtils.success(res);
    }

    /**
     * 下线接口
     */
    @PostMapping("/offline")
    public BaseResponse<Boolean> offlineInterfaceInfo(@RequestBody IdRequest idRequest, HttpServletRequest request) {
        if (idRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = interfaceInfoService.offlineInterfaceInfo(idRequest, request);
        return ResultUtils.success(res);
    }

    /**
     * 根据id查询接口信息
     */
    @PostMapping("/get")
    public BaseResponse<InterfaceInfo> getInterfaceInfo(@RequestBody IdRequest idRequest) {
        if (idRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = idRequest.getId();
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id<=0");
        }
        InterfaceInfo interfaceInfo = interfaceInfoService.getById(id);
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口不存在");
        }
        return ResultUtils.success(interfaceInfo);
    }

    /**
     * 在线调用接口
     */
    @PostMapping("/invoke")
    public BaseResponse<Object> invokeInterfaceInfo(@RequestBody InterfaceInfoInvokeRequest interfaceInfoInvokeRequest, HttpServletRequest request) {
        if (interfaceInfoInvokeRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Object res = interfaceInfoService.invokeInterfaceInfo(interfaceInfoInvokeRequest, request);
        return ResultUtils.success(res);
    }
}
